/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solitaireofdoom.solitaire;

import java.util.Vector;
import java.util.Random;

/**
 * The Card class is used to keep information about a single card, but it contains static information such as the letters for the various suits and the height and width of each card.
 * 
 * @author Administrator
 */
public class Deck
{
    private Vector<Card> cards;

    public Deck()
    {
        cards = new Vector<Card>();
    }

    /**
     * Adds a card to the back of the vector after checking to see if the card
     * is already contained within the vector.
     *
     * @param c <i>Card</i>
     */
    public void addCard(Card c){
        if (!cards.contains(c))
            cards.add(c);
    }

    /**
     * Method Fills the pile with face down cards.
     */
    public void fillPile()
    {
        this.fillPile(false);
    }

    /**
     * Method Clears stack of cards and fills the pile with cards of
     * the facing provided
     *
     * @param facing <i>boolean</i>
     */
    public void fillPile(boolean facing)
    {
        for (int r = 1; r < 14; r++)
        {
            for (int s = 0; s < 4; s++)
            {
                try
                {
                    cards.add(new Card(Card.Suits[s], r, facing));
                }
                catch (CardException e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    public void clearPile()
    {
        cards.clear();
    }

    /**
     * Shuffles the vector cards
     */
    public void shuffle()
    {
        Vector<Card> oldCards = cards;
        Vector<Card> newCards = new Vector<Card>();
        Random r = new Random(System.currentTimeMillis());

        while (oldCards.size() > 0)
        {
            newCards.add(oldCards.remove(r.nextInt(oldCards.size())));
        }

        cards = newCards;
    }

    /**
     * Method Reverses the deck
     */
    public void reverse()
    {
        Vector<Card> oldCards = cards;
        Vector<Card> newCards = new Vector<Card>();

        for (int i = oldCards.size() - 1; i >= 0; i--)
        {
            newCards.add(oldCards.elementAt(0));
        }
        
        cards = newCards;
    }

    /**
     * Returns a card at index
     *
     * @param index <i>int</i>
     * @return cards(index)
     */
    public Card getCard(int index)
    {
        return cards.elementAt(index);
    }

    /**
     * <p>Creates new stack of cards, taking the top cards off the deck to create
     * the new stack.</p> <p>[facetype == 0] all cards are face down</p> <p>[facetype == 1] all cards are face up</p> <p>[facetype == 2] all cards are face down expect the top card.</p>
     *
     * @param count How many cards do you want?
     * @param faceType <p>[facetype == 0] all cards are face down</p> <p>[facetype == 1] all cards are face up</p> <p>[facetype == 2] all cards are face down expect the top card.</p>
     * @return A new deck of count <i>count</i>
     */
    public Deck getCards(int count, int faceType)
    {
        Deck newDeck = new Deck();

        for (int i = 0; i < count && i < this.cards.size(); i++)
        {
            Card c = this.cards.get(i);

            if (faceType == 0)
                c.setFacing(false);
            else if (faceType == 1)
                c.setFacing(true);
            else if (faceType == 2)
            {
                if (i == count - 1)
                    c.setFacing(true);
                else
                    c.setFacing(false);
            }

            newDeck.addCard(c);
            this.cards.removeElementAt(i);
        }

        return newDeck;
    }

    /**
     * removes card at <i>index</i>
     * 
     * @param index
     */
    public void removeCard(int index)
    {
        this.cards.removeElementAt(index);
    }

    /**
     * Returns size of the deck.
     *
     * @return size <b>int</b>
     */
    public int size()
    {
        return cards.size();
    }
}
